using System.IO;
using System.Text;
using System.Collections.Generic;
 
using UnityEngine;
using UnityEditor;
 
namespace SK.Framework
{
    public sealed class GUIIcon : EditorWindow
    {
        [MenuItem("SKFramework/Editor/GUI/GUI Icon/Open GUIIcon")]
        private static void OpenGUIIcon()
        {
            GetWindow<GUIIcon>().Show();
        }
 
        [MenuItem("SKFramework/Editor/GUI/GUI Icon/Load GUIIcon")]
        private static void LoadGUIIcon()
        {
            List<Texture> icons = new List<Texture>(Resources.FindObjectsOfTypeAll<Texture>());
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < icons.Count; i++)
            {
                sb.Append(icons[i].name);
                if (i != icons.Count - 1)
                {
                    sb.Append("\r");
                }
            }
            //找到GUIIcon脚本 为了将txt文本存储到相同路径 也可以指定其他路径
            string path = AssetDatabase.FindAssets(typeof(GUIIcon).Name)[0];
            path = AssetDatabase.GUIDToAssetPath(path).Replace(".cs", ".txt");
            File.WriteAllText(path, sb.ToString());
            AssetDatabase.Refresh();
        }
 
        private Vector2 scrollPosition;
        private string[] names;
        private string searchContent = "";
        private const float width = 50f;
 
        private void OnGUI()
        {
            if (names == null)
            {
                //获取存储txt文本的路径
                string path = AssetDatabase.FindAssets(typeof(GUIIcon).Name)[0];
                path = AssetDatabase.GUIDToAssetPath(path).Replace(".cs", ".txt");
                if (File.Exists(path))
                {
                    string content = File.ReadAllText(path);
                    names = content.Split('\r');
                }
                EditorGUILayout.HelpBox($"File {path} not exists.", MessageType.Error);
            }
            else
            {
                GUILayout.BeginHorizontal("Toolbar");
                {
                    GUILayout.Label("Search:", GUILayout.Width(50));
                    searchContent = GUILayout.TextField(searchContent, "SearchTextField");
                }
                GUILayout.EndHorizontal();
 
                scrollPosition = GUILayout.BeginScrollView(scrollPosition);
                {
                    List<string> matchNames = new List<string>();
                    for (int i = 0; i < names.Length; i++)
                    {
                        if (names[i].ToLower().Contains(searchContent.ToLower()))
                        {
                            matchNames.Add(names[i]);
                        }
                    }
                    int count = Mathf.RoundToInt(position.width / (width + 3f));
                    for (int i = 0; i < matchNames.Count; i += count)
                    {
                        GUILayout.BeginHorizontal();
                        for (int j = 0; j < count; j++)
                        {
                            int index = i + j;
                            if (index < matchNames.Count)
                            {
                                if (GUILayout.Button(EditorGUIUtility.IconContent(matchNames[index]), GUILayout.Width(width), GUILayout.Height(30)))
                                {
                                    EditorGUIUtility.systemCopyBuffer = matchNames[index];
                                    Debug.Log(matchNames[index]);
                                }
                            }
                        }
                        GUILayout.EndHorizontal();
                    }
                }
                GUILayout.EndScrollView();
            }
        }
    }
}